#include "stdafx.h"
#include "QGHThreadObject.h"
#include <QtCore/QCoreApplication>

#define EXIT_CODE 0x1fff

QGHThreadObject::QGHThreadObject()
{
	m_pThread = new QThread(this);

	m_pThreadTaskItem = NULL;
}

QGHThreadObject::~QGHThreadObject()
{
	if(m_pThread)
	{
		delete m_pThread;
	}
}

void QGHThreadObject::assignTaskItem( QGHThreadTaskItem* pThreadTaskItem )
{
	Q_ASSERT(pThreadTaskItem);
	m_pThreadTaskItem = pThreadTaskItem;

	if(m_pThread && !m_pThread->isRunning())
	{
		m_pThread->start();
		moveToThread(m_pThread);
	}
}

QGHThreadTaskItem* QGHThreadObject::getThreadTaskItem()
{
	return m_pThreadTaskItem;
}


void QGHThreadObject::doProcess( QObject * pEngine,QObject * target )
{
	if(this == target)
	{
		m_pThreadTaskItem->run(target);

		emit processFinished(target);
	}
}

void QGHThreadObject::reset()
{
	m_pThreadTaskItem = NULL;
}




int QGHThreadObject::execEvent()
{
	return m_loopEvent.exec();
	//return  QCoreApplication::exec();
}

void QGHThreadObject::exitEvent()
{
	return m_loopEvent.exit(EXIT_CODE);
	//return QCoreApplication::exit(EXIT_CODE);
}
